﻿if (typeof (dxrApp) === 'undefined')
    dxrApp = angular.module('dxr.controls', []);

dxrApp.directive('dxrDropdownlist', function () {
    return {
        scope: {
            "option": '=dOption'
        },
        restrict: 'AE',
        replace: true,
        template: function (elem, attrs) {

            var classes = $dxr.isNullOrEmpty(attrs.class) ? "" : attrs.class;
            var style = $dxr.isNullOrEmpty(attrs.style) ? "" : attrs.style;

            var content = "";

            content += '<div class="ui search compact selection dropdown ' + classes + '" style="' + style + '">';
            content += '    <input type="hidden" name="type" value="{{value}}" />';
            content += '    <div class="default text">请选择</div>';
            content += '    <i class="dropdown icon"></i>';
            content += '    <div class="menu">';
            content += '        <div class="item" ng-repeat="item in list" data-value="{{item[option.valueKey]}}">{{ item[option.textKey] }}</div>';
            content += '    </div>';
            content += '</div>';

            return content;
        },
        link: function ($scope, elem, attrs) {

            $scope.value = "";

            $scope.list = [];

            var defaultOption = {
                valueKey: "Value",
                textKey: "Description",
            };

            $scope.option = $.extend(true, defaultOption, $scope.option);

            if (!$dxr.isNullOrEmpty(attrs["ngModel"])) {
                var names = attrs["ngModel"].split(".");

                var key = "$parent";
                for (var i = 0; i < names.length; i++) {
                    key += "." + names[i];
                }

                $scope.$watch(key, function (newValue, oldValue) {
                    if ($dxr.isNullOrEmpty(newValue)) {
                        $(elem).dropdown("clear");
                        return;
                    }

                    if (newValue != oldValue && newValue != $scope.value)
                        $(elem).dropdown("set selected", newValue);
                });
            }

            $(elem).dropdown({
                onChange: function (value, text, $selectedItem) {
                    $scope.value = value;
                    var bindObj = $scope.$parent;
                    var bindField = "";
                    if (!$dxr.isNullOrEmpty(attrs["ngModel"])) {
                        for (var i = 0; i < names.length; i++) {
                            if (i == names.length - 1)
                                bindField = names[i];
                            else
                                bindObj = bindObj[names[i]];
                        }
                        bindObj[bindField] = value;
                    }

                    if ($scope.option.onChange)
                        $scope.option.onChange(value, text, $selectedItem);
                }
            });

            var _self = {
                setList: function (list) {
                    $scope.list = list;
                },
                setValue: function (value) {
                    $(elem).dropdown("set selected", value);
                    $scope.value = value;
                }
            };

            if ($dxr.isNullOrEmpty(attrs["dxrDropdownlist"])) {
                $scope.$parent.dxrDropdownlist = _self;
            } else {
                $scope.$parent[attrs["dxrDropdownlist"]] = _self;
            }

        },
    };
});